Skip to content

Method: lessThan(Expression, Expression)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.criteria;
16:
17: import cz.cvut.kbss.jopa.model.CriteriaQueryImpl;
18: import cz.cvut.kbss.jopa.model.query.criteria.*;
19: import cz.cvut.kbss.jopa.query.criteria.expressions.*;
20: import cz.cvut.kbss.jopa.sessions.CriteriaBuilder;
21: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
22:
23: import java.util.Arrays;
24:
25: public class CriteriaBuilderImpl implements CriteriaBuilder {
26:
27: private final UnitOfWorkImpl uow;
28:
29: public CriteriaBuilderImpl(UnitOfWorkImpl uow) {
30: this.uow = uow;
31: }
32:
33: @Override
34: public <T> CriteriaQuery<T> createQuery(Class<T> resultClass) {
35: return new CriteriaQueryImpl<>(new CriteriaQueryHolder<>(resultClass), uow.getMetamodel(), this);
36: }
37:
38: @Override
39: public Expression<Integer> count(Expression<?> x) {
40: if (x == null) throw new IllegalArgumentException("Aggregate function cannot be applied to null expression.");
41: if (x instanceof AbstractPathExpression) {
42: return new ExpressionCountImpl<>(Integer.class, (AbstractPathExpression) x, this);
43: }
44: throw new IllegalArgumentException("Aggregate function can be applied only to path expressions.");
45: }
46:
47:
48: @Override
49: public <T> ParameterExpression<T> parameter(Class<T> paramClass) {
50: if (paramClass == null) throw new IllegalArgumentException("Class must be defined.");
51: return new ParameterExpressionImpl<>(paramClass, null, this);
52: }
53:
54: @Override
55: public <T> ParameterExpression<T> parameter(Class<T> paramClass, String name) {
56: if (paramClass == null) throw new IllegalArgumentException("Class must be defined.");
57: return new ParameterExpressionImpl<>(paramClass, name, this);
58: }
59:
60: @Override
61: public <T> Expression<T> literal(T value) {
62: if (value == null) throw new IllegalArgumentException("Literal cannot be null.");
63: return new ExpressionLiteralImpl<>(value, this);
64: }
65:
66: @Override
67: public Expression<String> literal(String value, String languageTag) {
68: if (value == null) throw new IllegalArgumentException("Literal cannot be null.");
69: return new ExpressionLiteralImpl<>(value, languageTag, this);
70: }
71:
72: @Override
73: public Order asc(Expression<?> x) {
74: return new OrderImpl(x);
75: }
76:
77: @Override
78: public Order desc(Expression<?> x) {
79: return new OrderImpl(x, false);
80: }
81:
82:
83: @Override
84: public Predicate and(Expression<Boolean> x, Expression<Boolean> y) {
85: return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(x, y), this);
86: }
87:
88: @Override
89: public Predicate and(Predicate... restrictions) {
90: if (restrictions.length == 1) return new SimplePredicateImpl(restrictions[0], this);
91: else return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(restrictions), this);
92: }
93:
94: @Override
95: public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {
96: return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(x, y), this);
97: }
98:
99: @Override
100: public Predicate or(Predicate... restrictions) {
101: if (restrictions.length == 1)
102: return new SimplePredicateImpl(Predicate.BooleanOperator.OR, restrictions[0], this);
103: else return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(restrictions), this);
104: }
105:
106: @Override
107: public Predicate equal(Expression<?> x, Expression<?> y) {
108: return new SimplePredicateImpl(new ExpressionEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
109: }
110:
111: @Override
112: public Predicate equal(Expression<?> x, Object y) {
113: return new SimplePredicateImpl(new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this), this);
114: }
115:
116: @Override
117: public Predicate equal(Expression<?> x, String y, String languageTag) {
118: return new SimplePredicateImpl(new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, languageTag, this), this), this);
119: }
120:
121: @Override
122: public Predicate notEqual(Expression<?> x, Expression<?> y) {
123: return new SimplePredicateImpl(new ExpressionNotEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
124: }
125:
126: @Override
127: public Predicate notEqual(Expression<?> x, Object y) {
128: return new SimplePredicateImpl(new ExpressionNotEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this), this);
129:
130: }
131:
132: @Override
133: public Predicate like(Expression<String> x, Expression<String> pattern) {
134: return new SimplePredicateImpl(new ExpressionLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this), this);
135: }
136:
137: @Override
138: public Predicate like(Expression<String> x, String pattern) {
139: return new SimplePredicateImpl(new ExpressionLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this), this), this);
140: }
141:
142: @Override
143: public Predicate notLike(Expression<String> x, Expression<String> pattern) {
144: return new SimplePredicateImpl(new ExpressionNotLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this), this);
145: }
146:
147: @Override
148: public Predicate notLike(Expression<String> x, String pattern) {
149: return new SimplePredicateImpl(new ExpressionNotLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this), this), this);
150: }
151:
152: @Override
153: public Predicate not(Expression<Boolean> restriction) {
154: return wrapExpressionToPredicateWithRepair(restriction).not();
155: }
156:
157: @Override
158: public <T> In<T> in(Expression<? extends T> expression) {
159: return new ExpressionInImpl<>(expression, this);
160: }
161:
162: @Override
163: public <T> In<T> notIn(Expression<? extends T> expression) {
164: In<T> inExpression = new ExpressionInImpl<>(expression, this);
165: inExpression.not();
166: return inExpression;
167: }
168:
169: @Override
170: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x, Expression<? extends Y> y) {
171: return new SimplePredicateImpl(new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
172: }
173:
174: @Override
175: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x, Y y) {
176: return new SimplePredicateImpl(new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this), this);
177: }
178:
179: @Override
180: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x, Expression<? extends Y> y) {
181: return new SimplePredicateImpl(new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
182: }
183:
184: @Override
185: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x, Y y) {
186: return new SimplePredicateImpl(new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this), this);
187: }
188:
189: @Override
190: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Expression<? extends Y> y) {
191: return new SimplePredicateImpl(new ExpressionLessThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
192: }
193:
194: @Override
195: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y) {
196: return new SimplePredicateImpl(new ExpressionLessThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this), this);
197: }
198:
199: @Override
200: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x, Expression<? extends Y> y) {
201: return new SimplePredicateImpl(new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
202: }
203:
204: @Override
205: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x, Y y) {
206: return new SimplePredicateImpl(new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this), this);
207: }
208:
209:
210: /**
211: * Method wraps given boolean expression to Predicate and if path expression occur, it wrap it to ExpressionEqualsImpl before.
212: * For example:
213: * {@literal Expression<Boolean> expression = factory.get("attributeName");}
214: * Looks like boolean expression but in fact it is not boolean expression, so we need to fix this.
215: *
216: * @param expression - boolean or path expression
217: * @return Expression wrapped in Predicate
218: */
219: public Predicate wrapExpressionToPredicateWithRepair(Expression<Boolean> expression) {
220: if (expression instanceof Predicate) {
221: return (Predicate) expression;
222: } else if (expression instanceof AbstractPathExpression) {
223: return new SimplePredicateImpl(new ExpressionEqualImpl((AbstractExpression) expression, (AbstractExpression) this.literal(true), this), this);
224: } else {
225: return new SimplePredicateImpl(expression, this);
226: }
227: }
228:
229: }